<?php

Yii::import('application.models._base.BaseOrders');


class Orders extends BaseOrders
{
  public $criteria;
  private $_cactivedataproviderarray;
  public $productVendorPrice;
  public $ordersVendorPrice;
  public $originalProductId;
  public $compensateProductId;
  
  public static function model($className=__CLASS__) {
    return parent::model($className);
  }
  
  public function rules() {
    return array(
        array('auction_id, date_won, price, quantity, total_price, status, created, customer_id', 'required'),
        array('date_won, quantity, status, created, paid_time, payment_method, packing_time, modified, payment_log_id, record_api_id, type, customer_grade, expired, defaulted, remind, payment_waiting, old_free_type', 'numerical', 'integerOnly'=>true),
        array('auction_id, price, total_price, shipping_method, customer_id, shipping_id, billing_id, shipping_fee, shipping_handling, vendor_price, product_vendor_price,tax_fee', 'length', 'max'=>10),
        array('shipping_tracking_num', 'length', 'max'=>45),
        array('shipping_comment, merchant_notes, shipping_fee_api, shipping_info, billing_info, youchooseit_text', 'safe'),
        array('id, auction_id, now_auction_id, payment_id, date_won, price, quantity, total_price, status, first_name, last_name, phone_code, phone, mobile_code, mobile, created, shipment_first_name, shipment_last_name, shipment_address, shipment_address2, shipment_region, shipment_country, shipment_zip_code, shipment_state_id, shipping_tracking_num, shipping_comment, shipping_method, billing_address, billing_region, billing_zip_code, billing_country, billing_state_id, merchant_notes, customer_id, shipping_id, billing_id, paid_time, shipping_fee, payment_method, packing_time, modified, shipping_fee_api, payment_log_id, record_api_id, type, shipping_handling, shipping_info, billing_info, customer_grade, expired, defaulted, remind, youchooseit_text, payment_waiting, vendor_price, old_free_type, product_vendor_price, coupon_id, discount, order_html, order_print_html, invoice_number, user_comment, invoice, shipped, deleted, quick_buy, product_quick_buy, shipping_reference, swap_to_amazon, swap_to_walmart, auction_title, auction_brand_name, auction_sku, auction_vendor_id, shipping_company, auction_current_price', 'safe'),
        array('id, auction_id, now_auction_id, payment_id, date_won, price, quantity, total_price, status, first_name, last_name, phone_code, phone, mobile_code, mobile, created, shipment_first_name, shipment_last_name, shipment_address, shipment_address2, shipment_region, shipment_country, shipment_zip_code, shipment_state_id, shipping_tracking_num, shipping_comment, shipping_method, billing_address, billing_region, billing_zip_code, billing_country, billing_state_id, merchant_notes, customer_id, shipping_id, billing_id, paid_time, shipping_fee, payment_method, packing_time, modified, shipping_fee_api, payment_log_id, record_api_id, type, shipping_handling, shipping_info, billing_info, customer_grade, expired, defaulted, remind, youchooseit_text, payment_waiting, vendor_price, old_free_type, product_vendor_price, coupon_id, discount, order_html, order_print_html, invoice_number, user_comment, invoice, shipped, deleted, quick_buy, product_quick_buy, shipping_reference, swap_to_amazon, swap_to_walmart, auction_title, auction_brand_name, auction_sku, auction_vendor_id, shipping_company, auction_current_price', 'safe', 'on'=>'search'),
    );
  }
  
  public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'oauction'      =>array(self::BELONGS_TO,'Auctions','auction_id'),
        'amazoncard' =>array(self::MANY_MANY,'Products','compensate(order_id,product_id)','on' => 'amazoncard_amazoncard.enable = '.ConstService::ENABLE),
    );
  }
  
  public function afterSave(){
//    $reportService = new ReportService();
//    $attributes = $reportService->setCustomerId($this->customer_id)->initialAffiliateTrackingReportAttributes();
    
//    $this->getVendorPrice();
//    $vendorprice = $this->search();
//    $attributes['active_customer'] = $this->customer_id;
//    $attributes['auctions_revenue'] = $this->total_price;
//    $attributes['orders_id'] = $this->id;
//    $attributes['orders_status'] = $this->status;
//    $attributes['orders_vendor_price'] = $vendorprice->ordersVendorPrice;
//    $attributes['product_vendor_price'] = $vendorprice->productVendorPrice;
//    $attributes['original_product_id'] = $vendorprice->originalProductId;
//    $attributes['compensate_product_id'] = $vendorprice->compensateProductId;
//    $attributes['orders_type'] = $this->type;
    
//    $m = new ReportAffiliateTracking;
//    $m->attributes = $attributes;
//    $m->addAffiliateCodeUac();

//         $reportService = new ReportService();
//         $attributes = $reportService->setCustomerId($this->customer_id)->initialAffiliateTrackingReportAttributes2();
//         $attributes['orders_id']                = $this->id;
//         $attributes['orders_status']            = $this->status;
//         $attributes['orders_type']              = $this->type;
//         $attributes['auctions_revenue']      = $this->total_price;
//         $attributes['shipping_fee']        = $this->shipping_fee;
//         //加入report
//         $r = new ReportAffiliateTrackingNew();
//         $r->attributes = $attributes;
//         $r->addAffiliateCodeUac();
  }
  
  public function afterUpdate($orders){
//    $reportService = new ReportService();
//    $attributes = $reportService->setCustomerId($orders->customer_id)->initialAffiliateTrackingReportAttributes2();
//    $attributes['orders_id']                = $orders->id;
//    $attributes['orders_status']            = $orders->status;
//    $attributes['orders_type']              = $orders->type;
//         $attributes['auctions_revenue']      = $orders->total_price;
//         $attributes['shipping_fee']        = $orders->shipping_fee;
//    //加入report
//    $r = new ReportAffiliateTrackingNew();
//    $r->attributes = $attributes;
//    $r->addAffiliateCodeUac();
  }
  
  public function setCriteria($criteria){
    $this->criteria = $criteria;
    return $this;
  }
  
  public function setCActiveDataProviderArray($cactivedataproviderarray){
    foreach ($cactivedataproviderarray as $key => $value) {
      $this->_cactivedataproviderarray[$key] = $value;
    }
    return $this;
  }
  
  public function search() {
    
    $this->_cactivedataproviderarray['criteria'] = $this->criteria;
    return new CActiveDataProvider($this, $this->_cactivedataproviderarray);
  }
  public function getAuctionInfo(){
      $auctionInfo=AuctionInfo::model()->findByPk($this->auction_id);
      return $auctionInfo;
  }
  public function search_my(){
      $criteria = new CDbCriteria;
      $criteria->addCondition('customer_id='.yii::app()->user->id);
      $criteria->addCondition('type!=2');
      $criteria->order="t.created desc";
      return  $a=new CActiveDataProvider($this, array(
          'criteria' => $criteria,
          'pagination'=>array(
              'pageSize'=>20, //当前页数显示记录条数
          ),
      
      ));
  }
  public function getSearchCondition(){
    $criteria = new CDbCriteria;
    
    $criteria->compare('id', $this->id);
    $criteria->compare('auction_id', $this->auction_id, true);
    $criteria->compare('date_won', $this->date_won);
    $criteria->compare('price', $this->price, true);
    $criteria->compare('quantity', $this->quantity);
    $criteria->compare('total_price', $this->total_price, true);
    $criteria->compare('status', $this->status);
    $criteria->compare('created', $this->created);
    $criteria->compare('shipping_tracking_num', $this->shipping_tracking_num, true);
    $criteria->compare('shipping_comment', $this->shipping_comment, true);
    $criteria->compare('shipping_method', $this->shipping_method, true);
    $criteria->compare('merchant_notes', $this->merchant_notes, true);
    $criteria->compare('customer_id', $this->customer_id, true);
    $criteria->compare('shipping_id', $this->shipping_id, true);
    $criteria->compare('billing_id', $this->billing_id, true);
    $criteria->compare('paid_time', $this->paid_time);
    $criteria->compare('shipping_fee', $this->shipping_fee, true);
    $criteria->compare('payment_method', $this->payment_method);
    $criteria->compare('packing_time', $this->packing_time);
    $criteria->compare('modified', $this->modified);
    $criteria->compare('shipping_fee_api', $this->shipping_fee_api, true);
    $criteria->compare('payment_log_id', $this->payment_log_id);
    $criteria->compare('record_api_id', $this->record_api_id);
    $criteria->compare('type', $this->type);
    $criteria->compare('shipping_handling', $this->shipping_handling, true);
    $criteria->compare('shipping_info', $this->shipping_info, true);
    $criteria->compare('billing_info', $this->billing_info, true);
    $criteria->compare('customer_grade', $this->customer_grade);
    $criteria->compare('expired', $this->expired);
    $criteria->compare('defaulted', $this->defaulted);
    $criteria->compare('remind', $this->remind);
    $criteria->compare('youchooseit_text', $this->youchooseit_text, true);
    $criteria->compare('old_free_type', $this->old_free_type);
    $criteria->compare('payment_waiting', $this->payment_waiting);
    
    $this->criteria = $criteria;
    return $criteria;
    
  }
  
  public function getVendorPrice(){
    
    $criteria = new CDbCriteria;
    $columns = array(
      'if(compensate.product_id is null,`products`.`vendor_price`,ifnull(`compensateProducts`.`vendor_price`,0)) as productVendorPrice',
      'IFNULL(orders.vendor_price,0) as ordersVendorPrice',
      'IFNULL(`products`.`id`,0) as originalProductId',
      'IFNULL(compensate.product_id,0) as compensateProductId',
    );
    
    $criteria->select = implode(',', $columns);
    $criteria->compare('id', $this->id);
    $criteria->compare('type', 1);
    $criteria->join = 'LEFT JOIN auctions ON auctions.id = orders.auction_id LEFT JOIN products ON products.id = auctions.product_id LEFT JOIN compensate ON compensate.order_id = orders.id and compensate.enable = 1 LEFT JOIN products as compensateProducts ON compensateProducts.id = compensate.product_id';
    
    $this->criteria = $criteria;
    return $criteria;
  }

    public function beforeSave(){
        if($save = parent::beforeSave()){
            if($this->status == '6'){
                $this->packing_time = time();
            }
        }
        return $save;
    }
    public  function  order_save($arr,$arr_fee_value,$model_auctionbidlog) {
    
    	$this->auction_id=$arr['id'];
    	$this->now_auction_id=0;
    	$this->date_won=$arr['created'];
    	$this->created=time();
    	$this->price=$arr['winner_price'];
    	$this->quantity=1;
    	$this->total_price=$arr['winner_price']+$arr_fee_value['shipping_fee'];
    	$this->status=1;
    	$this->customer_id=$arr['winner_id'];
    	$this->shipping_fee=$arr_fee_value['shipping_fee'];
    	$this->type=1;
    	$this->vendor_id=$arr_fee_value['vendor'];
    	//$this->shipping_handling=$arr_fee_value['billing_fee'];
    	$this->save();
    }
}